package com.deng.mapper;

import com.deng.pojo.Employee;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @create 2020-12-24 15:09
 */
@Mapper
public interface EmployeeMapper {
    @Results({
            @Result(property = "department", javaType = com.deng.pojo.Department.class, column = "department_id",
                    many = @Many(select = "com.deng.mapper.DepartmentMapper.getDepartmentById"))
    })
    @Select("select `id`,`lastName`,`email`,`gender`,`department_id`,`date` from t_employee")
    List<Employee> getAllEmployees();


    @Insert("insert into t_employee(`lastName`,`email`,`gender`,`department_id`,`date`) values(#{lastName},#{email},#{gender},#{department.id},#{date})")
    int addEmployee(Employee employee);

    @Results({
            @Result(property = "department", javaType = com.deng.pojo.Department.class, column = "department_id",
                    many = @Many(select = "com.deng.mapper.DepartmentMapper.getDepartmentById"))
    })
    @Select("select `id`,`lastName`,`email`,`gender`,`department_id`,`date` from t_employee where id=#{id}")
    Employee getEmployeeById(@RequestParam("id") Integer id);

    @Update("update t_employee set `lastName`=#{lastName},`email`=#{email},`gender`=#{gender},`department_id`=#{department.id},`date`=#{date} where id=#{id}")
    int updateEmployee(Employee employee);

    @Delete("delete from t_employee where id=#{id}")
    int deleteEmployeeById(@RequestParam("id") Integer id);
}
